$(function (){
    const inputUtil = new Input_util();
    inputUtil.popUpsView();
    inputUtil.popUpsInsert()
    let popUpsDown = $(".pop-ups-down");
    popUpsDown.on("click",function () {
        $(".recycle-bin-window").css("display","none")
    })

    $(".search-img").on("click",function (){
        const val=$(".search-input input").val()
        window.location.href="/www.xiaomi.com/background/home?menuId=101&warehouseMc="+val;
    })

    $(".searchCommodity-img").on("click",function (){
        const val=$(".search-searchCommodity-input input").val()
        window.location.href="/www.xiaomi.com/background/home?menuId=101&commodityWindowStatic=1&commodityMc="+val;
    })
    $(".revise-button").on("click",function (){
        const ID=$(this).attr("commodity-warehouse-id")
        console.log($(this).attr("index"));
        editRow(ID,$(this).attr("index"))
    })
    //仓库商品
    $(".view").on("click",function (){
        $(".window").css("display","block")
        const ID=$(this).attr("warehouse-id")
        window.location.href="/www.xiaomi.com/background/home?menuId=101&commodityWindowStatic=1&warehouse-id="+ID;
    })
    $(".shut-down").on("click",function (){
        $(".window-insert").css("display","none")
    })

    //仓库商品修改
    function editRow(rowId,index) {
        const inventoryCell = $($(".inventory")[index]);
        const limitCell = $($(".purchase")[index]);
        const editButton = $($(".revise-button")[index]);
        if (inventoryCell.children("input").length === 0) {
            // 进入编辑模式
            const inventoryValue = inventoryCell.html();
            const limitValue = limitCell.html();
            inventoryCell.html('<input style="width: 50px;text-align: center;" value="' + inventoryValue + '">');
            limitCell.html('<input style="width: 50px;text-align: center;" value="' + limitValue + '">');
            editButton.text("保存");
        } else {
            // 保存修改
            const inventoryInput = inventoryCell.children("input");
            const limitInput = limitCell.children("input");
            const inventory=inventoryInput.val()
            const limit=limitInput.val()
           if(!isNaN(Number(inventory)) && !isNaN(Number(limit))){
               inventoryCell.text(inventoryInput.val());
               limitCell.text(limitInput.val());
               editButton.text("修改");
               const commodityWarehouse = new Commodity_warehouse();
               commodityWarehouse.reviseCommodityWarehouse(inventory,limit,rowId).then(res => {
                   location.reload();
               })
           }
        }
    }

})
